﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kugar.Core.BaseStruct;
using Kugar.Core.Data;
using Kugar.Core.ExtMethod;
using Kugar.ERPFramework.BLLInterface;
using Kugar.ERPFramework.Entity;
using Kugar.Core.Data.MSSQLHelper;
using Kugar.ERPFramework.ServiceCommon;
using Kugar.Core.Data.MSSQLHelper;
using System.Runtime.Remoting.Messaging;

namespace Kugar.ERPFramework.ServiceSystemBLL
{
    public abstract class BaseModule : MarshalByRefObject, ICustomBLLModule, IDisposable
    {
        private IProjectInstance _instance = null;

        public BaseModule(IProjectInstance instance)
        {
            _instance = instance;
        }

        public virtual string Name { get { return "BaseModule"; } }

        public ResultReturn Execute(string methodID, params object[] paramList)
        {
            return (ResultReturn)this.FastInvoke(methodID, paramList);
        }

        protected SQLDataHelper GetConnection(string name=null)
        {
            return _instance.GetOtherConnection(name);

            //if (string.IsNullOrWhiteSpace(name))
            //{
            //    return (SQLDataHelper)_pool.Value.Take();
            //}
            //else
            //{
            //    //TODO:返回其他链接
            //    return null;
            //}
        }


        protected virtual SQLDataHelper CurrentConnection
        {
            get 
            {


                return _instance.CurrentConnection;

                //SQLDataHelper _conn=(SQLDataHelper)CallContext.GetData("CurrentConnection");

                //if (_conn==null)
                //{
                //    lock (_connLockerObj)
                //    {
                //        _conn=(SQLDataHelper)CallContext.GetData("CurrentConnection");

                //        if (_conn==null)
                //        {

                //            _instance.CurrentConnection
                //            _conn=(SQLDataHelper) .Value.Take();
                //        }
                //    }
                //}

                //return _conn;
            }
        }

        private bool _isDisposing = false;
        private bool _isDisposed = false;
        public void Dispose()
        {
            if (!_isDisposing && !_isDisposed)
            {
                _isDisposing = true;

                try
                {
                    Disposing();
                    _isDisposed = true;
                }
                catch (Exception)
                {

                    throw;
                }
                finally
                {
                    _isDisposing = false;
                }
            }
        }

        protected virtual void Disposing()
        {
            _pool.Value.Dispose();
        }

        #region ICustomBLLModule 成员


        public IProjectInstance ProjectInstance
        {
            get { return _instance; }
        }

        #endregion
    }
}
